package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;
@Autowired
private StatementMapper statementMapper;
    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }


    @Override
    public void save(Appointment appointment) {
        appointment.setCreateTime(new Date());
        appointmentMapper.insert(appointment);
    }

    @Override
    public void update(Appointment appointment) {
        Appointment old = appointmentMapper.selectByPrimaryKey(appointment.getId());
        if (!Appointment.STATUS_APPOINTMENT.equals(old.getStatus())) {
            throw new BusinessException("出现了一点小问题");
        }
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }




    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            appointmentMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }
    //到店
    public void arrival(Long id,Date date,Integer integer){
        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
            throw new BusinessException("系统状态出错");
        }
        appointmentMapper.updateStatusArrival(id,date,integer);
    }

    //取消
    public void cancel(Long id,Integer integer){
        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);

        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
            throw new BusinessException("系统状态出错");
        }
        appointmentMapper.updateStatus(id,integer);
    }

    @Override
    public Long generateStatement(Long appointmentId) {
        //根据id 查预约单
        Appointment appointment = this.get(appointmentId);
        //根据预约单id 查询结算单有没有数据
        Statement statement = statementMapper.selectByAppointmentId(appointmentId);
        if (statement==null) {
            //没有数据就新建一条数据 保存进去
            statement = new Statement();
            statement.setCreateTime(new Date());
            statement.setActualArrivalTime(appointment.getActualArrivalTime());
            statement.setAppointmentId(appointmentId);
            statement.setCarSeries(appointment.getCarSeries());
            statement.setCustomerName(appointment.getCustomerName());
            statement.setCustomerPhone(appointment.getCustomerPhone());
            statement.setInfo(appointment.getInfo());
            statement.setLicensePlate(appointment.getLicensePlate());
            statement.setServiceType(appointment.getServiceType());
            statementMapper.insert(statement);
        }
        //修改预约表状态为已结算
            appointmentMapper.updateStatus(appointmentId,Appointment.STATUS_SETTLE);

        return statement.getId();
    }

}
